<template>
  <div class="tree-item">
    <div class="left">
      <!-- 利用插槽，渲染用户提供的图标 -->
      <slot name="icon">
        <!-- 插槽的后备内容，默认的图标 -->
        <svg-icon icon-class="tree" />
      </slot>
      <span class="depart-name">{{ data.name }}</span>
    </div>
    <div class="right">
      <!-- 负责人 -->
      <span>{{ data.manager }}</span>
      <!-- 操作 -->
      <el-dropdown @command="onCommand">
        <span class="el-dropdown-link"> 操作<i class="el-icon-arrow-down el-icon--right"></i></span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-if="!data.pid" command="add"> 添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="data.id" command="edit">修改部门</el-dropdown-item>
          <el-dropdown-item v-if="data.id" command="del">删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TreeItem',
  props: {
    // 每个树形节点要渲染的数据
    data: {
      type: Object,
      required: true
    }
  },
  methods: {
    onCommand(type) {
      // console.log('ok', type)
      this.$emit('opt', { type, departId: this.data.id })
    }
  }
}
</script>

<style scoped lang="scss">
.depart-name {
  font-size: 12px;
  margin-left: 5px;
}

.tree-item {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  .left {
    color: #000;
  }
  .right {
    .el-dropdown {
      font-size: 12px;
      margin-left: 20px;
    }
  }
}
</style>
